home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / jav503.zip / BOUNCEAP.JAV < prev    next >
Text File  |  1996-06-04  |  4KB  |  211 lines

  1. // -[KeepHeading]-
  2.  
  3.  
  4. // -[Copyright]-
  5.  
  6. /**
  7.  * (c) Step Ahead Software Pty Ltd 1996. All rights reserved. Registered
  8.  * of JAVelin may use this applet in their web pages.
  9.  */
  10. import java.lang.*;
  11.  
  12.  
  13. // -[KeepBeforeClass]-
  14. import java.awt.Rectangle;
  15. import java.awt.Color;
  16. import java.awt.Graphics;
  17. import java.awt.Font;
  18. import java.awt.Event;
  19. import java.applet.Applet;
  20.  
  21.  
  22. // -[Class]-
  23.  
  24. /**
  25.  * @jTitle           BounceApplet
  26.  * @jAuthor          Chris Colman
  27.  * @jOverridability  can be overridden
  28.  * @jDescription
  29.  * BounceApplication is a JAVA applet that manages and displays a set
  30.  * of BounceAnimations.
  31.  * 
  32.  * @see              Runnable, Applet
  33.  */
  34. public 
  35. class BounceApplet extends Applet implements Runnable
  36. {
  37. // -[KeepWithinClass]-
  38.  
  39.  
  40. // -[Fields]-
  41.  
  42.  
  43.  
  44. /**
  45.  * A constant that stores the number of objects in the BounceAnimations
  46.  * array.
  47.  */
  48. protected static int NUMOBJS= 4;
  49.  
  50.  
  51.  
  52. /**
  53.  * An array of BounceAnimations. This is initially unallocated but the
  54.  * constructor allocates space for the array.
  55.  */
  56. protected BounceAnimation bounceAnimations[];
  57.  
  58.  
  59.  
  60. /**
  61.  * Thread of execution that cycles through the BounceAnimations advancing
  62.  * them to their next state.
  63.  */
  64. protected Thread bouncer= null;
  65.  
  66.  
  67.  
  68. /**
  69.  * Font used for message at the bottom.
  70.  */
  71. protected Font messageFont= new Font("Helvetica", Font.PLAIN, 12);
  72.  
  73.  
  74.  
  75. /**
  76.  * Font used for moving objects.
  77.  */
  78. protected Font movingFont= new Font("TimesRoman",Font.BOLD,24);
  79.  
  80.  
  81.  
  82. /**
  83.  * State of thread's execution.
  84.  */
  85. protected boolean threadSuspended= false;
  86.  
  87.  
  88.  
  89. // -[Methods]-
  90.  
  91. /**
  92.  * Describe here
  93.  */
  94. public void destroy()
  95. {
  96.     if ( bouncer != null )
  97.     {
  98.         bouncer.stop();
  99.         // wake up so can die
  100.         bouncer.resume();
  101.     }
  102. }
  103.  
  104. /**
  105.  * Initialises the applet.
  106.  */
  107. public void init()
  108. {
  109.     // Allocate space for the array of BounceAnimations
  110.     bounceAnimations = new BounceAnimation[NUMOBJS];
  111.  
  112.     // Resize the applet window ot 400x120 pixels
  113.     resize(400,120);
  114.  
  115.     // Set the current font to the message font field
  116.     setFont(messageFont);
  117.  
  118.     // Create the animations and add them to the array
  119.     bounceAnimations[0] = new BounceAnimation("Javelin: Slick, cool and very, very HOT!", 1, Color.red, 50, 20, 3);
  120.     bounceAnimations[1] = new BounceAnimation("Visual Classworks", -1, Color.blue, 100, 80, 1);
  121.     bounceAnimations[2] = new BounceAnimation("C++", 1, Color.green, 150, 60, 5);
  122.     bounceAnimations[3] = new BounceAnimation("Visual Object Oriented Development", -1, Color.yellow, 175, 40, 4);
  123. }
  124.  
  125. /**
  126.  * Starts the applet.
  127.  */
  128. public void start()
  129. {
  130.     System.out.println("Starting");
  131.  
  132.     if (bouncer == null) {
  133.         bouncer = new Thread(this);
  134.         bouncer.start();
  135.     }
  136.     else
  137.         if (bouncer.isAlive())
  138.             bouncer.resume();
  139. }
  140.  
  141. /**
  142.  * Called to stop the execution.
  143.  */
  144. public void stop()
  145. {
  146.     System.out.println("Stopping");
  147.  
  148.     if (bouncer != null) {
  149.         bouncer.suspend();
  150.     }
  151. }
  152.  
  153. /**
  154.  * Calls when a new thread is created. The thread dies when this function
  155.  * returned.
  156.  */
  157. public void run()
  158. {
  159.     while ( true ) {
  160.         try {
  161.             Thread.sleep(100);
  162.         } 
  163.         catch (InterruptedException e) {
  164.         }
  165.  
  166.         repaint();
  167.     }
  168. }
  169.  
  170. /**
  171.  * Called whenever a window needs updating.
  172.  */
  173. public void paint(Graphics g)
  174. {
  175.     Rectangle r = bounds();
  176.  
  177.  
  178.     Font oldF = g.getFont();
  179.     g.setFont(movingFont);
  180.  
  181.     for(int i = 0; i < NUMOBJS; i++)
  182.     {
  183.         bounceAnimations[i].advance(r.width);
  184.         bounceAnimations[i].paint(g);
  185.     }
  186.     g.setFont(oldF);
  187.  
  188.     g.drawString("Click to toggle the animation on/off", 10, 100);
  189. }
  190.  
  191.  
  192.  
  193.  
  194. /**
  195.  * Called when the mouse goes down on this object.
  196.  */
  197. public boolean mouseDown(Event evt, int x, int y)
  198. {
  199.     if (threadSuspended) {
  200.         bouncer.resume();
  201.     }
  202.     else {
  203.         bouncer.suspend();
  204.     }
  205.     
  206.     threadSuspended = !threadSuspended;
  207.  
  208.     return true;
  209. }
  210. }
  211.